package com.sc.socket.chatroom.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author shuc001
 * @Date 2019-02-05 11:55
 */
public class Client {
    public static void main(String[] args) {
        ServerInfo serverInfo = ClientSearcher.searchServer(10000);
        System.out.println(serverInfo);
        if(serverInfo!=null){
            TCPClient client=null;
            try {
                client= TCPClient.startWith(serverInfo);
                if(client==null){
                    return;
                }
                write(client);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(client!=null){
                    client.exit();
                }
            }
        }
    }

    private static void write(TCPClient client) throws IOException {
        //键盘输入
        InputStream keyboard = System.in;
        BufferedReader keyReader = new BufferedReader(new InputStreamReader(keyboard));
        //socket输出流

        do {
            String str = keyReader.readLine();
            client.send(str);
            if("byebye".equalsIgnoreCase(str)){
                break;
            }
        }while (true);
    }
}
